package ch.ffhs.services;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import ch.ffhs.activities.R;
import ch.ffhs.contacts.Contact;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

/**
 * The Class MessageService. Implements sending and receiving of alarm-messages.
 * 
 * @author mf
 * @author sw
 */
public class MessageService extends Service {
	private Integer timerPeriod;
	private Timer mTimer = null;
	private CountdownTimerTask sendCountdown = null;
	private Integer mCountdown = 5;
	private Integer messageCount = 0;
	private final IBinder mMessageBinder = new MessageBinder();
	private Handler mCallbackHandler;
	private ArrayList<Contact> contacts = new ArrayList<Contact>();
	private GpsPosition gpsPosition = null;

	public class MessageBinder extends Binder {
		public void setGpsPosition(GpsPosition gpsPosition) {
			MessageService.this.gpsPosition = gpsPosition;
		}

		public void setContacts(ArrayList<Contact> contacts) {
			MessageService.this.contacts.clear();
			MessageService.this.contacts.addAll(contacts);
		}

		public void setCallbackHandler(final Handler callback) {
			mCallbackHandler = callback;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mMessageBinder;
	}

	public boolean onUnbind(Intent intent) {
		mCallbackHandler = null;
		return super.onUnbind(intent);
	}

	public void onCreate() {
		super.onCreate();
		Log.d("MessageService", "onCreate");
		mTimer = new Timer("MessageTimer");
		if (ServiceManager.testMode) {
			timerPeriod = 1000;
		} else {
			timerPeriod = 1000 * 60;
		}
	}

	@Override
	public void onDestroy() {
		mTimer.cancel();
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d("MessageService", "onStartCommand");
		startTimer();
		return super.onStartCommand(intent, flags, startId);
	}

	private void sendMessage(Contact contact) {
		// senden eines Alarms an einen Notfall-Kontakt
		messageCount++;

		if (mCallbackHandler != null) {
			Log.d("MessageService", "SendMessage");

			final Message msg = new Message();
			final Bundle bundle = new Bundle();
			bundle.putString("SendMessage",
					"sending message to " + contact.getName());
			bundle.putInt("MessageCount", messageCount);
			msg.setData(bundle);
			mCallbackHandler.sendMessage(msg);

			String smsMessageBody = getString(R.string.sms_message_body);

			SmsSenderTask sms = new SmsSenderTask();
			sms.setContact(contact);
			sms.setGpsPosition(gpsPosition);
			sms.execute(smsMessageBody);
		} else {
			Log.d("MessageService",
					"receiveMessageFromContact - no callback handler");
		}
	}

	public void receiveMessageFromContact() {
		Log.d("MessageService", "receiveMessageFromContact");
		if (mCallbackHandler != null) {
			final Message msg = new Message();
			final Bundle bundle = new Bundle();
			bundle.putBoolean("AlarmDeactivated", true);
			msg.setData(bundle);
			mCallbackHandler.sendMessage(msg);
		} else {
			Log.d("MessageService",
					"receiveMessageFromContact - no callback handler");
		}
	}

	private class CountdownTimerTask extends TimerTask {
		@Override
		public void run() {
			Log.d("CountdownTimerTask", "run");

			if (mCountdown > 0) {
				mCountdown--;
			} else {
				stopTimer();
				Contact contact = contacts.get(messageCount);
				sendMessage(contact);
				if (messageCount < 4) {
					mCountdown = 5;
					startTimer();
				}
			}
		}
	}

	private void startTimer() {
		mTimer.cancel();
		mTimer = new Timer("MessageTimer");
		sendCountdown = new CountdownTimerTask();
		mTimer.scheduleAtFixedRate(sendCountdown, 0, timerPeriod);
	}

	private void stopTimer() {
		mTimer.cancel();
	}
}
